#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "kernel.h"
#include "all.h"

// 得到字符串真实长度
int get_length(char str[])
{
	char *p = str;
	int count = 0;
	while (*p++ != '\0')
	{
		count++;
	}
	return count;
}


void operate(){
	char order[20];
	strcpy(order,"ls");
	while(strcmp(order, "exit")){
		printf(">>$");
		scanf("%s", order);
		if(!strcmp(order, "format")){
            fileSystem_formatDisk();
            printf("成功\n");

		}else if(!strcmp(order, "delfile")){
			printf("请输入想要删除文件的inode\n");
			int blockNo;
			scanf("%d", &blockNo);
			fileSystem_free(blockNo);
			printf("已删除\n");

		}else if(!strcmp(order, "write")){
			printf("请输入想要写入的字符串\n");
			byte buffer[3000];
			scanf("%s",buffer);
			int file1 = fileSystem_create(get_length(buffer) + 1);
			int fileid1 = fileSystem_open(file1);
			printf("请输入想写入文件的偏移\n");
			int offset;
			scanf("%d",&offset);
			fileSystem_seek(fileid1, offset, SEEK_SET);   
			fileSystem_write(fileid1, buffer, get_length(buffer) + 1); 
			fileSystem_show();
			fileSystem_close(fileid1);

		}else if(!strcmp(order, "read")){
			printf("请输入想要读取第几个文件\n");
			int file1;
			scanf("%d", &file1);
			int fileid1 = fileSystem_open(file1);
			fileSystem_seek(fileid1, 0, SEEK_SET); 
			fileSystem_read(fileid1);
			fileSystem_close(fileid1);

		}else if(!strcmp(order, "fdisk")){
			printf("fdisk\n");
			printf("文件数目：%d\n",fileSystem.fileNum);
			printf("总block数目：%d\n",fileSystem.superBlock.size);
			printf("索引节点开始位置：%d\n",fileSystem.superBlock.isize);
			printf("未使用数据块的位置：%d\n",fileSystem.superBlock.freeList);

		}else if(!strcmp(order, "show")){
            fileSystem_show();

		}else if(!strcmp(order, "help")){
			printf("文件系统包含命令：\n");
			printf("format       格式化\n");
			printf("show         显示磁盘结构信息\n");
			printf("write        写入文件\n");
			printf("read         读出文件内容\n");
			// printf("delfile      删除block(删除文件还没实现，但是可以删除指定的block)\n");
			printf("exit         退出文件系统\n");
			printf("help         帮助\n");
			printf("fdisk        打印filesystem的信息\n");
			printf("\n");

		} else if(!strcmp(order, "exit")){
			continue;
		}else {
			printf("no such order!\nget help by input h\n");
		}
	}
	
}

int main(){

    // 初始化
    int errCode;
    errCode = init();
    if(errCode == 1)
        printf("Filesystem formatting failed");
    fileSystem_show();

    // 写入文件测试
    byte buffer[] = "write test";
	int file1 = fileSystem_create(sizeof(buffer));
	int fileid1 = fileSystem_open(file1);
    fileSystem_seek(fileid1, 0, SEEK_CUR);   
    fileSystem_write(fileid1, buffer, sizeof(buffer)); 
    fileSystem_show();
    fileSystem_close(fileid1);
    // fileSystem_free(1);
    // fileSystem_show();
  
	printf("文件系统包含命令：\n");
	printf("format       格式化\n");
	printf("show         显示磁盘结构信息\n");
	printf("write        写入文件\n");
	printf("read         读出文件内容(只实现了直接块无偏移读取)\n");
	// printf("delfile      删除block(删除文件还没实现，但是可以删除指定的block)\n");
	printf("exit         退出文件系统\n");
	printf("help         帮助\n");
	printf("fdisk        打印filesystem的信息\n");
	

    // 
    operate();

    return 0;
}